<template>
  <div>
         <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">

        <FormItem label="用户名：" prop="name">
            <Input v-model="formValidate.name" placeholder="请输入你的姓名"></Input>
        </FormItem>
        <FormItem label="手机号：" prop="phone">
            <Input v-model="formValidate.phone" placeholder="请输入你的手机号"></Input>
        </FormItem>        
        <FormItem label="密码：" prop="password">
            <Input v-model="formValidate.password" placeholder="请输入密码"></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">登录</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
        </FormItem>
    </Form>
  </div>
</template>

<script>
export default {
    data () {
          const validatePass=(rule,value,callback)=>{
              if(value === ''){
                  callback(new Error('密码不能为空！'))
              }else if(value=='123456'){
                   callback(new Error('密码过于简单，狗都不设！赶紧换一个！'))
              }else{
                  callback()
              }
          }
            return {
                formValidate: {
                    name: '',
                    phone:'',
                    password:''
                },
              
                ruleValidate: {
                    name: [
                        { required: true, message: '用户名不能为空！', trigger: 'blur' },
                        {min:2,max:12,message:'用户名必须为2~13位字符之间！'}
                    ],
                    phone:[
                         { required: true, message: '手机号不能为空！', trigger: 'blur' },
                        {pattern:/^1[3,4,5,6,7,8,9]\d{9}$/,message:'手机号格式有误！',trigger:'blur'}
                    ],
                    password:[
                          { required: true, message: '密码不能为空！', trigger: 'blur' },
                         { validator:validatePass, trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('Success!');
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        }
}
</script>

<style>

</style>